<?php
//http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL
require('database/connection.php');
require('libs/secure.php');
require_once('libs/log.php');
sec_session_start();
require_once('libs/recaptchalib.php');
$publickey = "6LdHzuESAAAAAB3v1zD6Lkr6Zq4JFReMKUzHSu3l";
$privatekey = "6LdHzuESAAAAALuPadF_thS-Cw0XglHj9EILs9fS";

define('USRRATE', 0);
if (USRRATE !== 1) {
    if (!isset($_SESSION))
        session_start();
    if (isset($_SESSION['uname']))
        define('RATER', $_SESSION['uname']);
}

ob_start();
if (!empty($_GET['mod'])) {
    $mod = $_GET['mod'];
} else {
    $mod = 'home';
}

$item_per_page= 20;
if(!empty($_GET['cat'])){
    $cat = $_GET['cat'];
    $results = mysqli_query($conn,"SELECT COUNT(*) FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t2.category_id=".$cat);
}else{
    $results = mysqli_query($conn,"SELECT COUNT(*) FROM app");
}

$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page); 

?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/mystyle.css">
    <link rel="stylesheet" type="text/css" href="css/categorymenu.css">
    <link rel="stylesheet" type="text/css" href="css/home.css">
    <link rel="stylesheet" type="text/css" href="css/register.css">
    <link rel="stylesheet" type="text/css" href="css/comment.css">

    <!-- Nivo Slider -->
    <link rel="stylesheet" type="text/css" href="css/nivo-slider.css">
    <link rel="stylesheet" href="themes/default/default.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="themes/light/light.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="themes/dark/dark.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="themes/bar/bar.css" type="text/css" media="screen" />

    <!-- Star Rating System -->
    <link href="ratingfiles/ratings.css" rel="stylesheet" type="text/css" />
    <script src="ratingfiles/ratings.js" type="text/javascript"></script>

    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/jquery.nivo.slider.pack.js" type="text/javascript"></script>
    <script src="js/jquery.timeago.js" type="text/javascript"></script>


    <title>Free App Store</title>

    <script type="text/javascript">
    $(document).ready(function() {
        $("abbr.timeago").timeago();
        swapValue =[];
        $(".input-text").each(function(i) {
            swapValue[i] = $(this).val();
            $(this).focus(function() {
                if ($(this).val() == swapValue[i]) {
                    $(this).val("");
                }
            }).blur(function() {
                if ($.trim($(this).val()) == "") {
                    $(this).val(swapValue[i]);
                }
            });
        });
    });


    $(document).ready(function() {

    var track_click = 0; //track user click on "load more" button, righ now it is 0 click
    
    var total_pages = <?php echo $total_pages; ?>;
    
    $('#results').load("modules/<?php echo $mod ?>.php<?php if(!empty($_GET['cat'])) echo"?cat=".$_GET['cat']?>", {'page':track_click}, function() {track_click++;}); //initial data to load


    $(".load_more").click(function (e) { //user clicks on button

        $(this).hide(); //hide load more button on click
        $('.animation_image').show(); //show loading image

        if(track_click <= total_pages) //user click number is still less than total pages
        {
            //post page number and load returned data into result element
            $.post('modules/<?php echo $mod ?>.php<?php if(!empty($_GET['cat'])) echo"?cat=".$_GET['cat']?>',{'page': track_click}, function(data) {

                $(".load_more").show(); //bring back load more button
                
                $("#results").append(data); //append data received from server
                
                //scroll page smoothly to button id
                $("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);
                
                //hide loading image
                $('.animation_image').hide(); //hide loading image once data is received

                track_click++; //user click increment on load button

            }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
                alert(thrownError); //alert with HTTP error
                $(".load_more").show(); //bring back load more button
                $('.animation_image').hide(); //hide loading image once data is received
            });
            
            
            if(track_click >= (total_pages-1)) //compare user click with page number
            {
                //reached end of the page yet? disable load button
                $(".load_more").attr("disabled", "disabled");
            }
        }

    });
});
</script>

<script type="text/javascript">
$(window).load(function() {
    $('#slider').nivoSlider({
        effect: 'fade'
    });
});
</script>
</head>
<body>
    
    <div id="wrap">
        <div id="top">
            <div id="login">
                <?php
                if(isset($_GET['error'])){
                    $error = $_GET['error'];
                    if($error == 1) echo "<script>alert('Password is not correct')</script>";    
                }
                
                if (isset($_SESSION['uname']) && !empty($_SESSION['uname'])) {
                    ?>
                    <a class="welcome" href="?mod=logout"> Log out</a>
                    <span class="welcome">Welcome <a href="?mod=profile"><?php echo htmlspecialchars($_SESSION['uname']) ?></a>&nbsp;&nbsp;&nbsp;</span>

                    <?php
                } else {
                    ?>
                    <div class="login-form">
                        <form action="modules/process_login.php" method="post" id="login_form">
                            Login: <input class="input-text" value="Name" id="login_name" name="name" style="color: #696969;"/>&nbsp;&nbsp;
                            <input class="input-text" type="password" id="login_pass" value="Password" name="p" style="color: #696969;"/>&nbsp;&nbsp;
                            <input type="submit" value="submit"/>&nbsp;&nbsp;
                            <a href="?mod=reg">Register</a>
                        </form>
                    </div>
                    <?php
                }
                ?>
            </div>
        </div>
        <div id="header">

            <div id="banner">
                <div class="slider-wrapper theme-light">
                    <div id="slider" class="nivoSlider">
                        <img src="img/Fruit_Ninja_Logo_HD_Wallpaper-Vvallpaper.Net.png" alt="img" />
                        <img src="img/plants vs joombies.jpg" alt="img" title="This is an example of a caption" />
                        <img src="img/angry birds.jpg" alt="img" />
                    </div>
                </div>
                <div id="htmlcaption" class="nivo-html-caption">
                    <strong>Welcome</strong> to the <em>APP</em> world <a href="#"> !!!</a>.
                </div>
            </div>
        </div>
        <?php if($mod != 'reg'){?>
        <div id="menu"> 
            <span id="nav">
                <a href="?mod=home">Home</a> <?php 
                if(!empty($_GET['cat'])) {
                    $cat_id = $_GET['cat'];
                    $sql = "SELECT * FROM category where id='".$cat_id."'";
                    $rs = mysqli_query($conn, $sql);
                    $r = mysqli_fetch_assoc($rs);
                    echo "> "."<a href=''>".$r['name']."</a>";
                }       
                ?>
            </span>

            <div id="search_space">
                <input class="search" type="text" name="search" id="search" onkeypress="if(event.keyCode==13) window.location='?mod=search&kw='+this.value"/>               <a onclick="window.location='?mod=search&kw='+document.getElementById('search').value" href="#" class="searchbtn">
                <img class="search-icon" src="img/search-icon.png" alt="search"/></a>
            </div>
        </div>
        <?php }?>
        
        <div id="content">
            <?php
            if ($mod == 'reg') {
                include('modules/register.php');
            } else if ($mod == 'profile') {
                include('modules/profile.php');
            }
            else {
                ?>
                <div id="left-content">
                    <div id="category">
                        <h2>Category</h2>
                        <ul id="CategoryMenuId" class="CategoryMenu">

                            <?php
                            $sql = "SELECT * FROM category";
                            $rs = mysqli_query($conn, $sql);
                            while ($r = mysqli_fetch_assoc($rs)) {
                                ?>
                                <li><a href="?cat=<?php echo $r['id']?>"><?php echo $r['name'] ?></a></li>    
                                <?php
                            }
                            ?>

                        </ul>
                    </div> 
                    <div id="top-category">
                        <h2>Top View</h2>
                        <ul id="CategoryMenuId" class="TopCategory">
                            
                            <?php
                            $sql = "SELECT * FROM app order by view desc limit 10";
                            $rs = mysqli_query($conn, $sql);
                            $stt=0;
                            while ($r = mysqli_fetch_assoc($rs)) {
                                $stt++;
                                ?>
                                <li><a href="?mod=detail&idapp=<?php echo $r['id'] ?>"><?php echo $stt."."?>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $r['name'] ?></a>

                                </li>    
                                <?php
                            }
                            ?>

                        </ul>
                    </div>
                </div>

                <div id="main-content">
                    <?php
                            //$count=0;
                    if($mod != 'home'){
                     include('modules/'.$mod.'.php');
                 }else{
                    
                    
                    
                    ?>
                    
                    <div id="results"></div>
                    
                    <br/>
                    <div align="center" class="load_more_div">
                        <button class="load_more" id="load_more_button">load More</button>
                        <div class="animation_image" style="display:none;"><img src="img/ajax-loader.gif"> Loading...</div>
                    </div>
                    <?php } ?>
                </div>
                <?php
            }
            ?>
        </div>

        <div id="footer">
            <h2 style="color: #000000"> Trư�?ng đại h�?c Quốc Gia TP HCM</h2>
            <h2 style="color: #000000"> �?ại h�?c Bách Khoa TP HCM</h2>
            <h2 style="color: #000000"> 268 Lý Thư�?ng Kiệt, F14, Q10</h2>
        </div>
    </div>


    <?php
// put your code here
    ob_flush();
    ?>

</body>
</html>